package com.masterclient.app.base;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.masterclient.app.R;

/**
 * 作者：sean
 * 创建时间：2017/8/23 17:25
 * 功能说明：
 */

public class ConfirmDialog extends Dialog {
    String title0, subTitle, title, specialTitle;
    String cancel;
    String confirm;
    int type;
    boolean specialStyle;
    private String confirmColor;

    public ConfirmDialog(@NonNull Context context, String title, String cancel, String confirm) {
        super(context, R.style.MyDialog);
        this.title = title;
        this.cancel = cancel;
        this.confirm = confirm;
    }
    public ConfirmDialog(@NonNull Context context, String title, String cancel, String confirm, String confirmColor) {
        this(context,title,cancel,confirm);
        this.confirmColor = confirmColor;
    }

    public ConfirmDialog(@NonNull Context context, boolean specialStyle, String title, String title0, String specialTitle, String cancel, String confirm) {
        super(context, R.style.MyDialog);
        this.specialStyle = specialStyle;
        this.title = title;
        this.title0 = title0;
        this.specialTitle = specialTitle;
        this.cancel = cancel;
        this.confirm = confirm;
    }

    public ConfirmDialog(@NonNull Context context, String title0, String title, String subTitle, String cancel, String confirm, String confirmColor) {
        this(context,false,title,title0,"",cancel,confirm);
        this.subTitle = subTitle;
        this.confirmColor = confirmColor;
    }

    public ConfirmDialog(@NonNull Context context, String title, String cancel, String confirm, int type) {
        super(context, R.style.MyDialog);
        this.title = title;
        this.cancel = cancel;
        this.confirm = confirm;
        this.type = type;
    }

    public ConfirmDialog(@NonNull Context context, String title, String cancel, String confirm, int type, String confirmColor) {
        this(context,title,cancel,confirm,type);
        this.confirmColor = confirmColor;
    }

    public ConfirmDialog(@NonNull Context context, String title0, String title, String cancel, String confirm, int type) {
        super(context, R.style.MyDialog);
        this.title0 = title0;
        this.title = title;
        this.cancel = cancel;
        this.confirm = confirm;
        this.type = type;
    }

    public ConfirmDialog(@NonNull Context context, String title0, String subTitle, String title, String cancel, String confirm, int type, String confirmColor) {
        super(context, R.style.MyDialog);
        this.title0 = title0;
        this.subTitle = subTitle;
        this.title = title;
        this.cancel = cancel;
        this.confirm = confirm;
        this.type = type;
        this.confirmColor = confirmColor;
    }

    private TextView tv_title0, tv_sub_title, tv_title, tv_cancel, tv_confirm;
    View vLine;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_confirm);
        tv_title0 = (TextView) findViewById(R.id.tv_title0);
        tv_sub_title = (TextView) findViewById(R.id.tv_sub_title);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_cancel = (TextView) findViewById(R.id.tv_cancel);
        tv_confirm = (TextView) findViewById(R.id.tv_confirm);
        vLine = findViewById(R.id.line);
        if (!TextUtils.isEmpty(confirmColor)){
            tv_confirm.setTextColor(Color.parseColor("#333333"));
        }
        switch (type) {
            case 1:
                tv_cancel.setVisibility(View.GONE);
                vLine.setVisibility(View.GONE);
                tv_confirm.setBackgroundResource(R.drawable.cash_common_dialog_confirm1_bkg);
                break;
        }

        if (!TextUtils.isEmpty(title0)) {
            tv_title0.setText(title0);
        }
        if (specialTitle != null && specialTitle.length() > 0) {
            specialTitle = specialTitle.replace("\\n","\n");
        }
        if (title != null) {
            tv_title.setVisibility(View.VISIBLE);
            if (specialStyle) {
                if (!TextUtils.isEmpty(title) && title.length() > 0 && title.contains("%s")) {
                    title = title.replace("\\n", "\n");
                    String frontTitle = "";
                    String endTitle = "";
                    frontTitle = title.substring(0, title.indexOf("%s"));
                    endTitle = title.substring(title.indexOf("%s"));
                    endTitle = endTitle.replace("%s", "");

                    SpannableString spannableString = new SpannableString(frontTitle + specialTitle + endTitle);
                    spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#F23051")), frontTitle.length(), frontTitle.length() + specialTitle.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
                    tv_title.setText(spannableString);
                } else {
                    if (!TextUtils.isEmpty(title) && title.length() > 0) {
                        tv_title.setText(title.replace("\\n", "\n"));
                    }
                }
            } else {
                if (!TextUtils.isEmpty(title) && title.length() > 0) {
                    tv_title.setText(title.replace("\\n", "\n"));
                }
            }
            tv_title.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
                if (tv_title.getLineCount() > 1) {
                    tv_title.setGravity(Gravity.LEFT);
                } else {
                    tv_title.setGravity(Gravity.CENTER);
                }
            });
        }else {
            tv_title.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(subTitle)) {
            tv_sub_title.setVisibility(View.VISIBLE);
            tv_sub_title.setText(subTitle);
        } else {
            tv_sub_title.setVisibility(View.GONE);
        }
        tv_cancel.setText(cancel);
        tv_confirm.setText(confirm);

        tv_cancel.setOnClickListener(v -> {
            if (onChooseListerner != null) {
                onChooseListerner.cancel();
            }
        });
        tv_confirm.setOnClickListener(v -> {
            if (onChooseListerner != null) {
                onChooseListerner.confirm();
            }
        });

    }

    public void setOneButton() {
        tv_cancel.setVisibility(View.GONE);
        vLine.setVisibility(View.GONE);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        WindowManager.LayoutParams windowparams = window.getAttributes();
        window.setGravity(Gravity.CENTER);
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(windowparams);
    }

    public void setOnChooseListerner(OnChooseListerner onChooseListerner) {
        this.onChooseListerner = onChooseListerner;
    }

    private OnChooseListerner onChooseListerner;

    public interface OnChooseListerner {
        public void cancel();

        public void confirm();
    }
}
